package com.ego.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.product.dao.CategoryBrandRelationDao;
import com.ego.product.po.Brand;
import com.ego.product.po.Category;
import com.ego.product.service.BrandService;
import com.ego.product.service.CategoryBrandRelationService;
import com.ego.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ego.product.po.CategoryBrandRelation;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ego
 * @description 品牌分类关联对应实现类
 * @author KamTang
 * @date 2022-11-13 12:56:14
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public List<CategoryBrandRelation> get(Long brandId) {
        return baseMapper.selectList(new QueryWrapper<CategoryBrandRelation>().eq("brand_id", brandId));
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long categoryId = categoryBrandRelation.getCategoryId();
        //1、查询详细名字
        Brand brand = brandService.getById(brandId);
        Category category = categoryService.getById(categoryId);

        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCategoryName(category.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateByIdAndName(Long brandId, String name) {
        CategoryBrandRelation relationEntity = new CategoryBrandRelation();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity, new UpdateWrapper<CategoryBrandRelation>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<Brand> getByCatId(Long catId) {
        List<CategoryBrandRelation> categoryId = this.list(new QueryWrapper<CategoryBrandRelation>()
                .eq("category_id", catId));
        return categoryId.stream()
                .map(item -> brandService.getById(item.getBrandId())).collect(Collectors.toList());
    }
}

